from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
import time

# Create an instance of Chrome WebDriver
driver = webdriver.Chrome()

# Navigate to the website with registration form
driver.get("https://demoqa.com/automation-practice-form")

# Enter invalid data into the form fields
driver.find_element(By.CLASS_NAME,"header-text").click()
time.sleep(5)
driver.find_element(By.ID,"firstName").send_keys("hua")
driver.find_element(By.ID,"lastName").send_keys("li")
driver.find_element(By.ID,"userEmail").send_keys("wejqfcwe")
driver.find_element(By.CLASS_NAME,"custom-control-label").click()
driver.find_element(By.ID,"userNumber").send_keys("123-abc")
time.sleep(5)
driver.find_element(By.XPATH,'/html/body/div[2]/div/div/div[2]/div[2]/div[2]/form/div[11]/div/button').send_keys(Keys.ENTER)

# Verify that appropriate error messages are displayed for each invalid field
# print (driver.find_element(By.XPATH,'//*[@id="userEmail"]').text)
# print (driver.find_element(By.XPATH,'//*[@id="userNumber"]').text)
assert driver.find_element(By.XPATH,'//*[@id="userEmail"]').text == ""
assert driver.find_element(By.XPATH,'//*[@id="userNumber"]').text == ""

# Enter valid data into the form fields
driver.find_element(By.ID,"userEmail").clear()
driver.find_element(By.ID,"userEmail").send_keys("lihua@example.com")
time.sleep(5)

driver.find_element(By.ID,"userNumber").clear()
driver.find_element(By.ID,"userNumber").send_keys("1234567890")
time.sleep(5)

# Submit the form and verify that the user is redirected to a success page
driver.find_element(By.XPATH,'/html/body/div[2]/div/div/div[2]/div[2]/div[2]/form/div[11]/div/button').send_keys(Keys.ENTER)
time.sleep(5)
assert driver.current_url == "https://demoqa.com/automation-practice-form"
assert driver.find_element(By.CSS_SELECTOR,"div#example-modal-sizes-title-lg").text == "Thanks for submitting the form"
# driver.quit()
